/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package ga.nqueens;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.operators.Probability;
import org.jheuristics.ga.operators.reproductors.DefaultMutation;

public class GoodSwapMutation extends DefaultMutation {

	/**
	 * TODO
	 *
	 * @param probability
	 */
	public GoodSwapMutation(Probability probability) {
		super(probability, 0);
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.operators.reproductors.DefaultMutation#mutate(java.lang.Object, org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
	 */
	protected Object mutate(Object objGens, GAStatus status, GAConfig config) {
        Probability probability = getProbability();
        return mute((List)objGens, probability, status, config);
	}
	
	protected List mute(List oriGgens, Probability probability, GAStatus status, GAConfig config) {
		
		List gens = new LinkedList((Collection) oriGgens);
        boolean mutated = false;
        // doing mutation
        for (int i = 0; i < oriGgens.size(); i++) {
            // should i mutate this position?
            if (probability.check(status, config)) {
                mutated = true; 
                doSwap(gens);
            }
        }
        if (mutated) {
            return oriGgens;
        }
        return null;
	}

	/**
	 * @param gens
	 * @return
	 */
	protected void doSwap(List<Integer> gens) {
		// swapping
		Board board = new Board(gens);
		int[] hitsByQueen = board.hitsByQueen();
		int posQueenMoreHitted = getMoreHitted(hitsByQueen);
		int posQueenLessHitted = getLessHitted(hitsByQueen);
		
		Integer more = new Integer(gens.get(posQueenMoreHitted));
		Integer less = new Integer(gens.get(posQueenLessHitted));
		
		gens.set(posQueenMoreHitted, less);
		gens.set(posQueenLessHitted, more);
	}

	protected int getLessHitted(int[] hitsByQueen) {
		
		int posLess = 0;
		int lessValue = -1;
		
		for (int i = 0; i < hitsByQueen.length; i++) {
			if(hitsByQueen[i] < lessValue) {
				lessValue = hitsByQueen[i]; 
				posLess = i;
			}
		}
		
		return posLess;
	}

	protected int getMoreHitted(int[] hitsByQueen) {
		
		int posMore = 0;
		int moreValue = -1;
		
		for (int i = 0; i < hitsByQueen.length; i++) {
			if(hitsByQueen[i] > moreValue) {
				moreValue = hitsByQueen[i]; 
				posMore = i;
			}
		}
		
		return posMore;
	}

}
